<template>
  <div class="line-chart-container">
    <!-- ECharts 容器 -->
    <div :id="chartId" style="width: 100%; height: 100%"></div>
  </div>
</template>

<script>
import * as echarts from "echarts";

export default {
  name: "LineChart",
  props: {
    // 图表数据，格式：{xAxis: [], seriesData: []}
    chartData: {
      type: Object,
      required: true,
      default: () => ({
        xAxis: [],
        seriesData: [],
      }),
    },
    // 图表标题
    title: {
      type: String,
      default: "",
    },
    // y 轴名称
    yAxisName: {
      type: String,
      default: "",
    },
    chartId: { type: String, default: "Line-chart" },
  },
  data() {
    return {
      // chartId: { type: String, default: "Line-chart" },
      myChart: null,
    };
  },
  mounted() {
    this.initChart();
  },
  watch: {
    // 监听数据变化，更新图表
    chartData(newVal) {
      this.updateChart(newVal);
    },
  },
  beforeDestroy() {
    // 销毁图表实例，释放资源
    if (this.myChart) {
      this.myChart.dispose();
      this.myChart = null;
    }
  },
  methods: {
    initChart() {
      const chartDom = document.getElementById(this.chartId);
      if (chartDom) {
        this.myChart = echarts.init(chartDom);
        this.updateChart(this.chartData);
      }
    },
    updateChart(data) {
      if (!this.myChart) return;
      const option = {
        title: {
          text: this.title,
          left: "left",
          textStyle: {
            color: "#fff", // 标题文字颜色，可根据主题调整
          },
        },
        tooltip: {
          trigger: "axis",
          axisPointer: {
            type: "line",
          },
        },
        xAxis: {
          type: "category",
          data: data.xAxis,
          axisLine: {
            lineStyle: {
              color: "#ccc", // x 轴轴线颜色
            },
          },
          axisLabel: {
            color: "#fff", // x 轴文字颜色
            fontSize: "16",
          },
        },
        yAxis: {
          type: "value",
          nameGap: 20, // 名称与轴线的距离
          name: this.yAxisName,
          nameTextStyle: {
            color: "#fff", // 坐标轴名称的颜色
            fontSize: "20", // 文字字体大小
            align: "right", // 文字水平对齐方式，默认自动（'left'，'center'，'right'）
            verticalAlign: "middle", // 文字垂直对齐方式，默认自动（'top'，'middle'，'bottom'
          },

          axisLine: {
            lineStyle: {
              color: "#ccc", // y 轴轴线颜色
            },
          },
          axisLabel: {
            color: "#fff", // y 轴文字颜色
            fontSize: "16",
          },
          splitLine: {
            show: false,
            // lineStyle: {
            //   color: "rgba(255,255,255,0.1)", // 网格线颜色，设为浅色透明
            // },
          },
        },
        series: [
          {
            name: this.title,
            type: "line",
            data: data.seriesData,
            smooth: false, // 开启平滑曲线
            lineStyle: {
              color: "#90EE90", // 折线颜色
              width: 2,
            },
            symbol: "circle", // 标记点样式
            symbolSize: 6,
            itemStyle: {
              color: "#90EE90", // 标记点颜色
            },
            areaStyle: {
              color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
                {
                  offset: 0,
                  color: "rgba(144, 238, 144, 0.3)",
                },
                {
                  offset: 1,
                  color: "rgba(144, 238, 144, 0)",
                },
              ]), // 填充渐变，营造透明区域效果
            },
          },
        ],
        // color: ["#90EE90"],
        grid: {
          left: "0",
          top: "10%",
          right: "0%",
          bottom: "0",
          containLabel: true,
        },
      };
      this.myChart.setOption(option);
    },
  },
};
</script>

<style scoped>
.line-chart-container {
  /* width: 100%;
  height: 300px; 可根据需求调整高度 */
  width: 100%;
  height: 100%;
  box-sizing: border-box;
}
</style>